var strFullPath=window.document.location.href;
var strPath=window.document.location.pathname;
var pos=strFullPath.indexOf(strPath);
var prePath=strFullPath.substring(0,pos);
var project = window.location.pathname.split("/")[1];
var url = prePath+"/"+project+"/workFile/getworkofstudent.wm";
$(function () {
    inithandInWork();
    $("#toptitle").text("提交作业");
});

function inithandInWork(){
    var data={};
    $.ajaxSetup({
        async : false
    });
    $.post(url,data,function (result) {
        var content="";
        if(result.works.length!=0){
            $("#nowork").hide();
            $("#demo").show();
            for(var i=0;i<result.works.length;i++){
                var ifhandin=0;
                for(var j=0;j<result.studentworks.length;j++){
                    if(result.works[i].jobTask_id==result.studentworks[j].jobTask_id){
                        content+="<tr><td>"+
                            "<div class='king-notice-box king-notice-success'></div></td>"+
                            "<td>"+ result.works[i].jobTask_id+"</td>"+
                            "<td>";
                        if(result.works[i].jobTask_reportTime==null){
                            content+="没有提交时间";
                        }else{
                            content+=result.works[i].jobTask_reportTime.substring(0,10);
                        }
                        content+="</td><td>";
                        if(result.works[i].jobTask_deadline==null){
                            content+="没有提交时间";
                        }else{
                            content+=result.works[i].jobTask_deadline.substring(0,10);
                        }
                        content+="</td>"+
                            "<td><div class='king-notice-box.king-notice-success'></div>"+result.works[i].jobTask_content+"</td>"+
                            "<td>" +
                            "<form class='form-horizontal'  method='post' enctype ='multipart/form-data' action='"+prePath+"/"+project+"/workFile/uploadfile.wm?jobtaskid="+result.works[i].jobTask_id+"'>" +
                            " <label><label><input type='file' name='uploadfile' onchange='check(this)'/></label> " +
                            "<label><button class='btn king-btn-demo king-btn king-round king-info' name='doupfile' id='"+result.works[i].jobTask_id+"'>上传</button></label></label> " +
                            "<label>文件名为：<a href='"+prePath+"/"+project+"/teacherWorkController/download.wm?workId="+result.studentworks[j].work_id+"'>"+result.studentworks[j].work_title+"</a></label></form> </td> </tr>";
                        ifhandin=1;
                        break;
                    }
                }
                if(ifhandin==0){
                    content+="<tr><td>"+
                        "<div class='king-notice-box king-notice-fail'></div></td>"+
                        "<td>"+ result.works[i].jobTask_id+"</td>"+
                        "<td>";
                    if(result.works[i].jobTask_reportTime==null){
                        content+="没有提交时间";
                    }else{
                        content+=result.works[i].jobTask_reportTime.substring(0,10);
                    }
                    content+="</td><td>";
                    if(result.works[i].jobTask_deadline==null){
                        content+="没有提交时间";
                    }else{
                        content+=result.works[i].jobTask_deadline.substring(0,10);
                    }
                    content+="</td>"+
                        "<td><div class='king-notice-box.king-notice-success'></div>"+result.works[i].jobTask_content+"</td>"+
                        "<td> " +
                        "<form class='form-horizontal'  method='post' enctype ='multipart/form-data' action='"+prePath+"/"+project+"/workFile/uploadfile.wm?jobtaskid="+result.works[i].jobTask_id+"'>" +
                        "<label><label><input type='file' name='uploadfile'onchange='check(this)'/></label> " +
                        "<label><button class='btn king-btn-demo king-btn king-round king-info' name='doupfile' id='"+result.works[i].jobTask_id+"'>上传</button></label></label> " +
                        "</form></td>" +
                        "</tr>";
                }
               
            }
            $("#datas").html(content);
        }else{
            $("#nowork").show();
            $("#demo").hide();
        }
    },"json");
    //设置分页
    $("#demo .holder").jPages({
        containerID : "datas",
        previous : "«",
        next : "»",
        perPage : 5,
        delay : 10,
        callback: function(pages, items){
            $('#total').text(items.count);
            $('#cur_page').text(pages.current);
            $('#total_page').text(pages.count);
        }
    });
}

function check(obj) {
    var filetype=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();//获得文件后缀名
    if(filetype!='.txt'&&filetype!='.zip'&&filetype!='.doc'&&filetype!='.rar'&&filetype!='.docx'){
        alert("请上传格式为txt、zip、doc、rar、docx的文件!");
        return false;
    }
    var fileSize = 0;
    var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
    if (isIE && !obj.files) {
        var filePath = obj.value;
        var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
        var file = fileSystem.GetFile (filePath);
        fileSize = file.Size;
    }else {
        fileSize = obj.files[0].size;
    }
    fileSize=Math.round(fileSize/1024*100)/100; //单位为KB
    if(fileSize<=0){
        alert("您选择的文件大小为0!");
        return false;
    }
    return true;
}